package trade.axht.control;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import trade.axht.dao.DepartmentDao;
import trade.axht.dao.EmployeeDao;
import trade.axht.entity.Employee;

@Controller
public class EmployeeHandler {
	
	@Autowired
	private EmployeeDao employeeDao;
	
	@Autowired
	private DepartmentDao departmentDao;
	
	@RequestMapping("/emps")
	public String list(Map<String , Object> map){
		map.put("employees", employeeDao.getAll());
		return "views/list";
	}
	
	@RequestMapping(value="/emp",method=RequestMethod.GET)
	public String input(Map<String, Object> map){
		map.put("departments", departmentDao.getDepartments());
		map.put("employee", new Employee());
		return "views/input";
	}
	
	@RequestMapping(value="/emp",method=RequestMethod.POST)
	public String save(Employee employee){
		employeeDao.save(employee);
		return "redirect:/emps";
	}
	
	@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
	public String delete(@PathVariable(value="id") Integer id){
		employeeDao.delete(id);
		return "redirect:/emps";
	}
	
	
	@ModelAttribute
	public void getEmployee(@RequestParam(value="id",required=false) Integer id,Map<String, Object> map){
		if(id!=null){
			map.put("employee", employeeDao.get(id));
		}
	
	}
	
	
	@RequestMapping(value="/emp", method=RequestMethod.PUT)
	public String update(@Valid Employee employee,BindingResult bindingResult){
		if(bindingResult.getErrorCount()>0){
			for(int i=0;i<bindingResult.getErrorCount();i++){
				System.out.println(bindingResult.getAllErrors().get(i));
			}
			return "redirect:/emps";
		}
		employeeDao.save(employee);	
		return "redirect:/emps";
	}
	
	@RequestMapping(value="/emp/{id}",method=RequestMethod.GET)
	public String edit(@PathVariable("id") Integer id,Map<String, Object> map){
		Employee employee=employeeDao.get(id);
		map.put("departments", departmentDao.getDepartments());
		map.put("employee", employee);
		return "views/input";
	}
	
	
	@RequestMapping(value="/testConversionServiceConverer")
	public String convert(Employee employee){
		System.out.println(employee);
		
		return "redirect:/emps";
	}
	
	@ResponseBody
	@RequestMapping(value="/testJson")
	public Collection<Employee> testJson(){
		return employeeDao.getAll();
	}
	
	@RequestMapping(value="/upload")
	public String upload(@RequestParam("desc") String desc,@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException{
		if(!file.isEmpty()){
			System.out.println("desc");
			File file2=new File("E:\\"+file.getOriginalFilename());
			if(!file2.exists()){
				file2.createNewFile();
			}
			file.transferTo(file2);
		}
		return "success";
	}
	
	/*@ExceptionHandler(ArithmeticException.class)
	public ModelAndView handlerArithmeticException(Exception exception ){
		System.out.println("出现异常:"+exception);
		ModelAndView mv=new ModelAndView("views/error");
		mv.addObject("exception",exception);
		return mv;
		
		
		
	}*/
	
	
	
	
	
	/**
	 * 1. 在 @ExceptionHandler 方法的入参中可以加入 Exception 类型的参数, 该参数即对应发生的异常对象
	 * 2. @ExceptionHandler 方法的入参中不能传入 Map. 若希望把异常信息传导页面上, 需要使用 ModelAndView 作为返回值
	 * 3. @ExceptionHandler 方法标记的异常有优先级的问题. 
	 * 4. @ControllerAdvice: 如果在当前 Handler 中找不到 @ExceptionHandler 方法来出来当前方法出现的异常, 
	 * 则将去 @ControllerAdvice 标记的类中查找 @ExceptionHandler 标记的方法来处理异常. 
	 */
	
	
	
	
	
	
	@RequestMapping("/testExceptionHandlerExceptionResolver")
	public String testExceptionHandlerExceptionResolver(@RequestParam("id") Integer id){
		System.out.println("result: "+(10/id));
		return "success";
	}
	
	
	
	
}
